package com.ruoyi.web.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SuperCartItem;
import com.ruoyi.system.service.ISuperCartItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 购物车Controller
 * 
 * @author ruoyi
 * @date 2025-08-13
 */
@RestController
@RequestMapping("/order/items")
public class SuperCartItemController extends BaseController
{
    @Autowired
    private ISuperCartItemService superCartItemService;

    /**
     * 查询购物车列表
     */
    @PreAuthorize("@ss.hasPermi('order:items:list')")
    @GetMapping("/list")
    public TableDataInfo list(SuperCartItem superCartItem)
    {
        startPage();
        List<SuperCartItem> list = superCartItemService.selectSuperCartItemList(superCartItem);
        return getDataTable(list);
    }

    /**
     * 导出购物车列表
     */
    @PreAuthorize("@ss.hasPermi('order:items:export')")
    @Log(title = "购物车", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SuperCartItem superCartItem)
    {
        List<SuperCartItem> list = superCartItemService.selectSuperCartItemList(superCartItem);
        ExcelUtil<SuperCartItem> util = new ExcelUtil<SuperCartItem>(SuperCartItem.class);
        util.exportExcel(response, list, "购物车数据");
    }

    /**
     * 获取购物车详细信息
     */
    @PreAuthorize("@ss.hasPermi('order:items:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(superCartItemService.selectSuperCartItemById(id));
    }

    /**
     * 根据用户id获取购物车列表
     *
     */
    @PreAuthorize("@ss.hasPermi('order:items:list')")
    @GetMapping(value = "/user/{userId}")
    public AjaxResult getInfoByUserId(@PathVariable("userId") Long userId)
    {
        return success(superCartItemService.selectCartByUserId(userId));
    }


    /**
     * 新增购物车
     */
    @PreAuthorize("@ss.hasPermi('order:items:add')")
    @Log(title = "购物车", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SuperCartItem superCartItem)
    {
        return toAjax(superCartItemService.insertSuperCartItem(superCartItem));
    }

    /**
     * 修改购物车
     */
    @PreAuthorize("@ss.hasPermi('order:items:edit')")
    @Log(title = "购物车", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SuperCartItem superCartItem)
    {
        return toAjax(superCartItemService.updateSuperCartItem(superCartItem));
    }

    /**
     * 根据用户id和商品id修改购物车内选定的商品数量
     */
    @PreAuthorize("@ss.hasPermi('order:items:edit')")
    @PutMapping("/UIdAndPId")
    public AjaxResult updateByUIdAndPId(@RequestBody SuperCartItem superCartItem)
    {
        return toAjax(superCartItemService.updateByUIdAndPId(superCartItem));
    }


    /**
     * 删除购物车
     */
    @PreAuthorize("@ss.hasPermi('order:items:remove')")
    @Log(title = "购物车", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(superCartItemService.deleteSuperCartItemByIds(ids));
    }

    /**
     * 修改购物车
     */
    @PreAuthorize("@ss.hasPermi('order:items:edit')")
    @PutMapping("/updateCart")
    public AjaxResult updateCart(@RequestBody SuperCartItem superCartItem)
    {
        return toAjax(superCartItemService.updateCart(superCartItem));
    }
}
